package scala

import java.sql.{Connection, DriverManager, PreparedStatement, ResultSet}

object Demo8Jdbc {

  def main(args: Array[String]): Unit = {

    //加载驱动
    Class.forName("com.mysql.jdbc.Driver")

    //创建链接
    val con: Connection = DriverManager.getConnection("jdbc:mysql://master:3306/student", "root", "123456")

    //执行sql查询数据
    val stat: PreparedStatement = con.prepareStatement("select * from student")

    //执行查询返回结果
    val resultSet: ResultSet = stat.executeQuery()

    //解析数据
    while (resultSet.next()) {

      //通过字段名取数据
      val id: Int = resultSet.getInt("id")
      val name: String = resultSet.getString("name")
      val age: Int = resultSet.getInt("age")
      val clazz: String = resultSet.getString("clazz")
      val gender: String = resultSet.getString("gender")

      println(id + "\t" + name + "\t" + age + "\t" + gender + "\t" + clazz)

    }

    con.close()

  }
}
